//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System.ServiceModel.Dispatcher;
using System.ServiceModel;


namespace SFWK.Core.Inspector
{
   

    /// <summary>
    /// Injects method call before and after service method call
    /// Doc: http://cgeers.wordpress.com/2008/11/09/wcf-extensibility-parameter-inspectors/
    /// </summary>
    public class InspectorParameter : System.ServiceModel.Dispatcher.IParameterInspector
    {

        #region Properties
        /// <summary>
        /// Type of operator passed to the service
        /// </summary>
        public readonly SFWK.Core.Inspector.InspectorAttribute.EOperatorType OperatorType;

        /// <summary>
        /// Position of the operator parameter
        /// </summary>
        public readonly int Position;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor with OperatorType
        /// </summary>
        /// <param name="operatorType"></param>
        /// <param name="position"></param>
        public InspectorParameter(SFWK.Core.Inspector.InspectorAttribute.EOperatorType operatorType, int position)  // url is a positional parameter
        {
            this.OperatorType = operatorType;
            this.Position = position;
        }

        /// <summary>
        /// Default Constructor
        /// </summary>
        public InspectorParameter()  // url is a positional parameter
        {
            this.OperatorType = SFWK.Core.Inspector.InspectorAttribute.EOperatorType.Operator;
            this.Position = 0;
        }
        #endregion

        #region IParameterInspector Members
        /// <summary>
        /// Method injected after the service call
        /// Dispose the data context at the end of the service call, when in wcf call mode
        /// </summary>
        /// <param name="operationName"></param>
        /// <param name="outputs"></param>
        /// <param name="returnValue"></param>
        /// <param name="correlationState"></param>
        public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
        {
            SFWK.Core.Unity.ServiceProvider.GetService<SFWK.Core.Inspector.IInspectAfterCall>().Inspect(operationName, outputs);
        }

        /// <summary>
        /// Method injected before the service call
        /// </summary>
        /// <param name="operationName"></param>
        /// <param name="inputs">Service's Parameter array</param>
        /// <returns></returns>
        public object BeforeCall(string operationName, object[] inputs)
        {
            //string userName = OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;

            SFWK.Core.Unity.ServiceProvider.GetService<SFWK.Core.Inspector.IInspectBeforeCall>().Inspect(this.OperatorType, this.Position, operationName, inputs);

            return null;
        }

        #endregion
    }

    
}

